package cn.wolfcode.service.impl;

import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.NoticeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UtilContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Notice notice) {
        //设置创建人
        notice.setCreator(UtilContext.getCurrentUser());
        //设置创建时间
        notice.setCreatTime(new Date());
        //设置未发布状态
        notice.setStatus(false);
        //先往数据库添加公告
        noticeMapper.insert(notice);
        //往中间表插入数据
        //查所有员工的id
        List<Employee> employees = employeeMapper.selectAll();
        for (Employee employee:employees) {
            Long eid = employee.getId();
            //往中间表设置公告初始
            noticeMapper.saveRead(notice.getId(),eid);
        }
    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice get(Long id, Boolean read) {
            noticeMapper.updateRead(id,UtilContext.getCurrentUser().getId(),read);
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Notice> listAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public PageInfo<Notice> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Notice> notices = noticeMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Notice>(notices);
    }

    @Override
    public void updataStatus(Long id, boolean status) {
         noticeMapper.updataStatus(id,status);
    }

       @Override
    public int selectCount() {
        //获得当前用户的id
           Long eid = UtilContext.getCurrentUser().getId();
           return noticeMapper.selectCount(eid);
    }

    @Override
    public List<Notice> selectByIdNotice(Long eid) {
        List<Notice> notices = noticeMapper.selectByIdNotice(eid);
        Date date = new Date();
        for (Notice notice:notices) {
            String str = "";
            String between = DateUtil.formatBetween(notice.getCreatTime(), date, BetweenFormatter.Level.MINUTE);
            if (between.contains("天")){
                str = between.substring(0,between.indexOf("天")+1);
            }else if (between.contains("小时")){
                str = between.substring(0,between.indexOf("小时")+2);
            }else if (between.contains("分")){
                str = between.substring(0,between.indexOf("分")+1);
            }
            //未读公告的发布时间
            notice.setNoticeTime(str);
        }

        return notices;
    }


}
